<?php

namespace App\Http\Controllers\Stock;

use App\Http\Controllers\ShopController;
use App\Models\Product\Order\Order as OrderModel;
use App\Models\Business\Business as BusinessModel;
use Illuminate\Support\Facades\Bus;

class Controller extends ShopController
{
    protected $timeList = [];

    public function __construct()
    {
        // 获取当前年份
        // $year = date('Y');

        // for($i = 1;$i <= 12;$i++)
        // {
        //     $start = strtotime(date('Y-m-01',strtotime($year . '-' . $i)));
        //     $end = strtotime(date('Y-m-t',strtotime($year . '-' . $i)));
        
        //     $this->timeList[] = [$start,$end];
        // }

        for($i = 1;$i <= 12;$i++)
        {
            $start = strtotime(date('Y-m-01',strtotime('2023' . '-' . $i)));
            $end = strtotime(date('Y-m-t',strtotime('2023' . '-' . $i)));
        
            $this->timeList[] = [$start,$end];
        }
    }

    public function total()
    {
        $OrderCount = OrderModel::count();

        $OrderMoney = OrderModel::where(['status' => 4])->sum('amount');

        $BusinessCount = BusinessModel::count();

        $data = [
            'OrderCount' => $OrderCount,
            'OrderMoney' => $OrderMoney,
            'BusinessCount' => $BusinessCount
        ];

        return $this->success('查询成功',$data);
    }

    public function business()
    {
        // 未认证
        $noCertifiedData = [];

        // 已认证
        $CertifiedData = [];

        foreach($this->timeList as $time)
        {
            $noCertifiedData[] = BusinessModel::where(['auth' => '0'])->whereBetween('createtime',$time)->count();

            $CertifiedData[] = BusinessModel::where(['auth' => '1'])->whereBetween('createtime',$time)->count();
        }

        $data = [
            'noCertified' => $noCertifiedData,
            'Certified' => $CertifiedData
        ];

        return $this->success('查询成功',$data);
    }
}
